﻿/// <reference path="app.js" />


irisApp.controller('RegisterController', [
    '$scope', '$http', function ($scope, $http) {
        $scope.user = {};

        $scope.registerUser = function () {
            $http.post('/account/register', $scope.user).success(function (data) {
                alert(data);
            });
        };
    }
]);

irisApp.controller('LoginController', [
    '$scope', '$http', function ($scope, $http) {
        $scope.user = {};

        $scope.login = function () {
            $http.post('/account/login', $scope.user).success(function (data) {
                alert(data);
            });
        };
    }
]);


irisApp.controller('AddRoleController', [
    '$scope', '$http', function ($scope, $http) {

        $scope.role = { name: '', parentRoleId: null };

        $http.get('/role/getuserroles').success(function (data) {
            $scope.rolesList = data;
        });

        $scope.selected = function () {
            console.log(this.obj.Name + ' is selected: ' + this.$selected);
            $scope.role.parentRoleId = this.obj.Id;
        };

        $scope.addRole = function () {

            //$scope.role.parentRoleId = $scope.parentRole.id;

            $http.post('/role/addrole', $scope.role).success(function (data) {
                alert(data);

                $scope.rolesList.push($scope.role);
            });

            console.log($scope.role);
        };
    }

]);

irisApp.controller('UserManagementController', [
    '$scope', '$http', function ($scope, $http) {

        $http.get('/role/MyAuthorizedRoles').success(function (data) {
            $scope.rolesList = data;
        });
    }


]);

irisApp.controller('UserListController', [
    '$scope', '$http', '$stateParams', function ($scope, $http, $stateParams) {

        var roleId = $stateParams.roleId;

        $http.get('/user/UsersList/' + roleId).success(function (data) {
            $scope.usersList = data;
        });
    }


]);


irisApp.controller('ManageUserRolesController', [
    '$scope', '$http', '$stateParams', function ($scope, $http, $stateParams) {




        var userId = $stateParams.userId;
        var myAuthorizedRoles;
        var userRoles;


        $scope.user = {};

        $scope.user.userId = userId;

        $scope.user.rolesId = [];

        $http.get('/role/MyAuthorizedRoles').success(function (data) {
            myAuthorizedRoles = data;
        }).then(function () {
            $http.get('/role/getuserroles/' + userId).success(function (data) {
                userRoles = data;
            }).then(function () {

                //console.log(myAuthorizedRoles);
                //console.log(userRoles);

                Compare(myAuthorizedRoles, userRoles);

                $scope.rolesList = myAuthorizedRoles;

            });
        });


        function Compare(t1, t2) {

            angular.forEach(t1, function (item1) {
                angular.forEach(t2, function (item2) {


                    if (item1.Id == item2.Id) {
                        //var tmp = { isInRole: true };
                        //angular.extend(item1, tmp);
                        ////$.extend(item1, tmp);
                        $scope.user.rolesId.push(item1.Id);

                    }

                });

                Compare(item1.ChildrenRoles, t2);

            });


        }




        $scope.selected2 = function () {

            console.log("aaaaaaa");

            var id = this.obj.Id;

            var idx = $scope.user.rolesId.indexOf(id);

            console.log("id:" + id);
            console.log(idx);

            // is currently selected
            if (idx > -1) {
                $scope.user.rolesId.splice(idx, 1);
            }

                // is newly selected
            else {
                $scope.user.rolesId.push(id);
            }
        };


        $scope.saveChanges = function () {

            console.log($scope.user);

            $http.post('/user/ChangeUserRoles', $scope.user).success(function (data) {

                alert(data);
            });

        };


    }


]);


irisApp.controller('PermissionController', [
    '$scope', '$http', '$stateParams', function ($scope, $http, $stateParams) {


        $http.get('/role/MyAuthorizedRoles').success(function (data) {
            $scope.myAuthorizedRoles = data;
        });


    }
]);



irisApp.controller('ChangePermissionsController', [
    '$scope', '$http', '$stateParams', function ($scope, $http, $stateParams) {

        var roleId = $stateParams.roleId;
        var myAuthorizedRoles;
        var rolePermissions;


        $scope.role = {};

        $scope.role.roleId = roleId;

        $scope.role.permissionsId = [];



        //$http.get('').success(function (data) {
        //    $scope.myAuthorizedRoles = data;
        //});



        $http.get('/permission/ChangePermissions/10/s').success(function (data) {
            myAuthorizedRoles = data;
            $scope.myAuthorizedRoles = myAuthorizedRoles;
        }).then(function () {
            $http.get('/permission/getrolepermissions/' + roleId).success(function (data) {
                rolePermissions = data;
            }).then(function () {

                //console.log(myAuthorizedRoles);
                //console.log(userRoles);

                angular.forEach(myAuthorizedRoles, function (role) {
                    //angular.forEach(role.Permissions, function(per) {
                    //    Compare(per, rolePermissions);
                    //});
                    Compare(role.Permissions, rolePermissions);

                });

                //Compare(myAuthorizedRoles,role)

                

            });
        });


        function Compare(t1, t2) {

            angular.forEach(t1, function (item1) {
                angular.forEach(t2, function (item2) {


                    if (item1.Id == item2.Id) {
                        //var tmp = { isInRole: true };
                        //angular.extend(item1, tmp);
                        ////$.extend(item1, tmp);
                        $scope.role.permissionsId.push(item1.Id);

                    }

                });

                Compare(item1.ChildrenPermissions, t2);

            });


        }




        $scope.selected2 = function (permissionId) {

            console.log("aaaaaaa");

            var id = permissionId;

            var idx = $scope.role.permissionsId.indexOf(id);

            console.log("id:" + id);
            console.log(idx);

            // is currently selected
            if (idx > -1) {
                $scope.role.permissionsId.splice(idx, 1);
            }

                // is newly selected
            else {
                $scope.role.permissionsId.push(id);
            }
        };







    }
]);